/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.easemob.chatui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.*;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.chatui.adapter.GroupAdapter;
import com.lyw.GeoLocationClient.App;
import com.lyw.GeoLocationClient.R;

import java.util.List;

public class GroupsFragment extends Fragment {
	private ListView groupListView;
	protected List<EMGroup> grouplist;
	private GroupAdapter groupAdapter;
	private InputMethodManager inputMethodManager;
	public static GroupsFragment instance;
	private ViewGroup mTitle;

	private boolean hideTitle;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_groups, container, false);
    }

	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		instance = this;
		inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
		grouplist =	EMGroupManager.getInstance().getAllGroups();
		mTitle = (ViewGroup) getView().findViewById(R.id.fragment_groups_title);
		if (hideTitle) {
			setHiddenTitle();
		}
		groupListView = (ListView)getView().findViewById(R.id.list);
		groupAdapter = new GroupAdapter(getActivity(), 1, grouplist);
		groupListView.setAdapter(groupAdapter);
		groupListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (position == groupAdapter.getCount() - 1) {
					//新建群聊
					startActivityForResult(new Intent(getActivity(), NewGroupActivity.class), 0);
				} else {
					
					//进入群聊
					Intent intent = new Intent(getActivity(), ChatActivity.class);
					// it is group chat
					intent.putExtra("chatType", ChatActivity.CHATTYPE_GROUP);
					intent.putExtra("groupId", groupAdapter.getItem(position - 1).getGroupId());
					startActivityForResult(intent, 0);
				}
			}

		});
		groupListView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (getActivity().getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
					if (getActivity().getCurrentFocus() != null)
						inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
								InputMethodManager.HIDE_NOT_ALWAYS);
				}
				return false;
			}
		});
        View newContact = getView().findViewById(R.id.iv_new_contact);
        if (App.isAdmin()) {

            newContact.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onPublicGroups(v);
                }
            });
        } else {
            newContact.setVisibility(View.GONE);
        }
    }

	public void setHiddenTitle() {
		if (mTitle != null) {
			mTitle.setVisibility(View.GONE);
		}
		hideTitle = true;
	}
	
	/**
	 * 进入公开群聊列表
	 */
	public void onPublicGroups(View view){
		startActivity(new Intent(getActivity(), PublicGroupsActivity.class));
	}
	
	@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		grouplist = EMGroupManager.getInstance().getAllGroups();
		groupAdapter = new GroupAdapter(getActivity(), 1, grouplist);
		groupListView.setAdapter(groupAdapter);
		groupAdapter.notifyDataSetChanged();
	}


	@Override
    public void onDestroy() {
		super.onDestroy();
		instance = null;
	}
	
	/**
	 * 返回
	 * @param view
	 */
//	public void back(View view){
//		finish();
//	}
}
